package com.glwan.fixedassetapp.ui.home.Activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.glwan.fixedassetapp.Adapter.OfflineAdapter;
import com.glwan.fixedassetapp.R;
import com.glwan.fixedassetapp.model.Asset;
import com.glwan.fixedassetapp.utils.CommonUtil;
import com.glwan.fixedassetapp.utils.KingDeeUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ProductActivity extends AppCompatActivity {

    private List<Asset> parentList = new ArrayList<>();

    private final static String TAG = "productActivity";

    private ExecutorService executorService = Executors.newSingleThreadExecutor();

    private ListView listView;

    private OfflineAdapter listableAdapter;


    private LinearLayout pullLoading;

    private LinearLayout noMoreData;
    private View loadingMore;

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            if (message.what == 1) {
                pullLoading.setVisibility(View.GONE);
                noMoreData.setVisibility(View.GONE);
            } else {
                noMoreData.setVisibility(View.VISIBLE);
            }
            listableAdapter.setAssetList(parentList);
            if (!parentList.isEmpty()) {
                Long notCountedCount = parentList.stream().filter(obj -> obj.getfHasCount().equals("待盘点")).count();
                Long errorCountedCount = 0L;
                Long countedCount = parentList.size() - notCountedCount - errorCountedCount;
                ((TextView) findViewById(R.id.notCountedCount)).setText(notCountedCount.toString());
                ((TextView) findViewById(R.id.countedCount)).setText(countedCount.toString());
                ((TextView) findViewById(R.id.errorCountedCount)).setText(errorCountedCount.toString());
            }
            return true;
        }
    });


    private Activity getActivity() {
        return this;
    }    //列表

    private void initData() {
        listView = findViewById(R.id.listView);
        listableAdapter = new OfflineAdapter(getActivity());
        listView.setAdapter(listableAdapter);
        loadingMore = LayoutInflater.from(getActivity()).inflate(R.layout.loading_more, null);
        pullLoading = loadingMore.findViewById(R.id.pullLoading);
        noMoreData = loadingMore.findViewById(R.id.noMoreData);
        listView.addFooterView(loadingMore, null, false);

        pullLoading.setVisibility(View.VISIBLE);
        listView.setOnItemLongClickListener((parent, view, position, id) -> {
            Map<String, Object> selectMap = new HashMap<>();
            Asset item = listableAdapter.getItem(position);
//            selectMap.put("fdId", item.getId());
            CommonUtil.intentConfirmDialog(
                    getActivity(),
                    InventoryTaskScanningActivity.class,
                    R.drawable.question,
                    getString(R.string.confirmInvetoryScan),
                    getString(R.string.confirm),
                    getString(R.string.cancel),
                    selectMap,
                    false);

            return true;
        });

        //请求后台数据
        CommonUtil.openProgressDialog(getActivity(), getString(R.string.loading));
        getInventData();
        CommonUtil.cancelProgressDialog(getActivity());

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_home_product);
        setTitle(getString(R.string.product_list));

        Toolbar toolbar = findViewById(R.id.moreToolbar);
        setSupportActionBar(toolbar);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        initData();
    }

    private void getInventData() {
        executorService.execute(() -> {
            // 执行网络请求
            List<Asset> assetList = KingDeeUtil.getInventData();
            parentList.addAll(assetList);
            Message message = handler.obtainMessage();
            message.what = 1;
            handler.sendMessage(message);
        });
    }

//    //异步获取盘点方案
//    private void getInventScheme() {
//        executorService.execute(() -> {
//            // 执行网络请求
////            inventShcemeMap = KingDeeUtil.getInventScheme();
//            inventSchemeList = KingDeeUtil.getInventSchemeNew();
//            for(InventScheme inventScheme:inventSchemeList){
//                inventShcemeMap.put(inventScheme.getInvent().getFID(),inventScheme.getFName());
//            }
//            // 在主线程中显示弹框
//            mainHandler.post(() -> showInventShcemeDialog());
//        });
//    }


    /**
     * 获取数据
     */
//    private void getDataNew(String number) {
//        //打开加载框
//        CommonUtil.openProgressDialog(getActivity(), getString(R.string.loading));
//        // 启动子线程执行任务
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                // 执行后台操作
//                List<Asset> assetList = KingDeeUtil.getProductListData(number);
//                parentList.addAll(assetList);
//                Message message = handler.obtainMessage();
//                message.what = 1;
//                handler.sendMessage(message);
//                CommonUtil.cancelProgressDialog(getActivity());
//            }
//        }).start();
//    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    //返回主界面
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }


}

